package cn.xinfei.xdecision.engine.runner.metadata;

import cn.xinfei.xdecision.common.model.enginex.po.Engine;
import cn.xinfei.xdecision.common.model.enginex.vo.EngineVersionInOutVarsRespVo;
import cn.xinfei.xdecision.common.service.enginex.EngineVersionService;
import com.google.common.collect.HashBasedTable;
import com.google.common.collect.Table;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Iterator;
import java.util.Set;

/**
 * 决策流出入参归总缓存
 */

@Slf4j
@Service
public class EngineInOutFieldLoader {


    @Resource
    private EngineVersionService engineVersionService;

    public Table<String, Long, EngineVersionInOutVarsRespVo> init(Table<String, Long, Engine> engineAndVersionMap) {
        log.info("EngineInOutFieldLoader start");
        Table<String, Long, EngineVersionInOutVarsRespVo> result = HashBasedTable.create();
        Set<Table.Cell<String, Long, Engine>> cells = engineAndVersionMap.cellSet();
        Iterator<Table.Cell<String, Long, Engine>> iterator = cells.iterator();
        while (iterator.hasNext()) {
            Table.Cell<String, Long, Engine> next = iterator.next();
            String engineCode = next.getRowKey();
            Long versionNo = next.getColumnKey();
            try {
                EngineVersionInOutVarsRespVo inOutVarList = engineVersionService.getInOutVarList(engineCode, versionNo,null);
                result.put(engineCode, versionNo, inOutVarList);
            } catch (Exception e) {
                log.error("reload engine input and output error. engineCode={}, error:{}", engineCode, e.getMessage(), e);
            }
        }
        log.info("EngineInOutFieldLoader end.size={}",result.size());
        return result;

    }

}
